﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Entities;

namespace XuZhou.SmartWaterConservancy.HeHu.Waters
{
    public class Category : AggregateRoot<Guid>
    {
        /// <summary>
        /// 分类编码
        /// </summary>
        public string Code { get; private set; }

        /// <summary>
        /// 分类名称
        /// </summary>
        public string Name { get; private set; }

        /// <summary>
        /// 父分类ID
        /// </summary>
        public Guid? ParentId { get; private set; }

        /// <summary>
        /// 父分类
        /// </summary>
        public virtual Category Parent { get; private set; }

        /// <summary>
        /// 子分类集合
        /// </summary>
        public virtual ICollection<Category> Children { get; private set; }

        /// <summary>
        /// 关联的站点类型
        /// </summary>
        public WaterStationType StationType { get; private set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string Remark { get; private set; }

        protected Category() { }

        public Category(
            Guid id,
            string code,
            string name,
            WaterStationType stationType,
            Guid? parentId = null,
            string remark = null) : base(id)
        {
            Check.NotNullOrWhiteSpace(code, nameof(code));
            Check.NotNullOrWhiteSpace(name, nameof(name));

            Code = code;
            Name = name;
            StationType = stationType;
            ParentId = parentId;
            Remark = remark;
            Children = new List<Category>();
        }

        /// <summary>
        /// 更新分类信息
        /// </summary>
        public void UpdateInfo(
            string name,
            WaterStationType stationType,
            Guid? parentId = null,
            string remark = null)
        {
            Check.NotNullOrWhiteSpace(name, nameof(name));

            Name = name;
            StationType = stationType;
            ParentId = parentId;
            Remark = remark;
        }

        /// <summary>
        /// 添加子分类
        /// </summary>
        public void AddChild(Category child)
        {
            Check.NotNull(child, nameof(child));

            if (child.Id == Id)
            {
                throw new BusinessException("不能将自身添加为子分类");
            }

            // 确保子分类与父分类类型一致
            if (child.StationType != StationType)
            {
                throw new BusinessException("子分类的站点类型必须与父分类一致");
            }

            Children.Add(child);
        }
    }
}
